package com.wl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.wl.mapper.ReportLabelMapper;
import com.wl.pojo.ReportLabel;
import com.wl.pojo.model.ReportLabelModel;
import com.wl.service.ReportLabelService;
import com.wl.util.MybatisUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: treeHole
 * @author: 吴磊
 * @create: 2021-07-06 18:17
 */
public class ReportLabelServiceImpl implements ReportLabelService {

    /**
     * 获取所有的举报标签
     *
     * @return
     */
    @Override
    public List<ReportLabelModel> getAllReportLabelOp() {
        ReportLabelMapper reportLabelMapper = MybatisUtils.getMapper(ReportLabelMapper.class);
        //1、获取所有的父级标签
        List<ReportLabel> parentLabel = reportLabelMapper.getALlReportLabel();
        //2、获取所有的子级标签
        List<ReportLabel> childLabel = reportLabelMapper.getAllChildReportLabel();
        List<ReportLabelModel> collect = parentLabel.stream().map(parent -> {
            ReportLabelModel parentLabelModel = new ReportLabelModel();
            BeanUtil.copyProperties(parent, parentLabelModel);
            ArrayList<ReportLabelModel> childLabelList = new ArrayList<>();
            for (ReportLabel child : childLabel) {
                ReportLabelModel childLabelModel = new ReportLabelModel();
                BeanUtil.copyProperties(child, childLabelModel);
                if (parentLabelModel.getReportId().equals(childLabelModel.getReportParentId())) {
                    childLabelList.add(childLabelModel);
                }
            }
            parentLabelModel.setChildLabel(childLabelList);
            return parentLabelModel;
        }).collect(Collectors.toList());
        return collect;
    }
}
